package fightplan.algorithmen.ko;

import java.awt.Color;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import fightplan.business.AbstractFighter;
import fightplan.business.FighterHolder;
import fightplan.business.FighterProvider;

@SuppressWarnings("serial")
public class FighterLabel extends JLabel implements PropertyChangeListener {
	private FighterProvider fighterProvider;
	private boolean dojoVisable;

	public static boolean clientMode = false;

	public FighterLabel(FighterProvider fighterProvider) {
		this(fighterProvider, false);
	}

	public FighterLabel(FighterProvider fighterProvider, boolean dojoVisable) {
		this.fighterProvider = fighterProvider;
		this.dojoVisable = dojoVisable;
		setOpaque(false);
		setBorder(BorderFactory.createLineBorder(Color.BLACK));
		setHorizontalAlignment(SwingConstants.LEFT);
		setFont(getFont().deriveFont(clientMode ? 14F : 10F));

		if (fighterProvider != null) {
			updateText();
			fighterProvider.addPropertyChangeListener(this);
		} else {
			setText("");
		}
	}

	/**
	 * Sollte nur zu Beginn geändert werden. Im ClientMode haben Die Label eine
	 * etwas grössere Schrift.
	 */
	public static void setClientMode(boolean clientMode) {
		FighterLabel.clientMode = clientMode;
	}

	public static int getLabelHeight() {
		return clientMode ? 25 : 18;
	}

	private void updateText() {
		AbstractFighter fighter = fighterProvider.getFighter();
		if (fighter == null) {
			setText("");
		} else {
			if (dojoVisable && fighter.getDojo() != null) {
				setText("  " + fighter.getName() + " (" + fighter.getDojo().getName() + ")");
			} else {
				setText("  " + fighter.getName());
			}
		}
		if (fighterProvider instanceof FighterHolder) {
			setFont(getFont().deriveFont(Font.BOLD));
		} else {
			setFont(getFont().deriveFont(Font.PLAIN));
		}
	}

	// PropertyChangeListener

	public void propertyChange(PropertyChangeEvent evt) {
		updateText();
	}

	// unserialisable

	private void writeObject(ObjectOutputStream out) throws IOException {
		throw new IllegalStateException("Should not be called");
	}

	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
		throw new IllegalStateException("Should not be called");
	}

}